<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['workerid'])){
		$userid=$this->model('worker')->get_userid(intval($_RQ['workerid']));
		$params['owner']=$userid;
		$return['params']['workerid']=$_RQ['workerid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_qun',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('username'=>$item['username'],'avatar'=>$item['avatar'],'nickname'=>$item['nickname']);
			if(!$item['workerid']){
				$item['workerid']=$this->model('worker')->get_workerid($item['owner']);
				pdo_update('jy_scrm_qun',array('workerid'=>$item['workerid']),array('id'=>$item['id']));
			}
			$item['num']=$item['member_list']?count(json_decode($item['member_list'])):0;
			pdo_update('jy_scrm_qun',array('num'=>$item['num']),array('id'=>$item['id']));
			$item['url']=custom_url('jy_scrm/gochat//id='.$item['id'],true);
			$item['welcomeurl']=custom_url('jy_scrm/gochat//fromqun=1&id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：需要企业员工先在企业微信APP端，创建企业微信群，这里才会同步获取到！<br>群欢迎语链接，请务必配置到群欢迎语里面自动回复（并让客户点击改链接！），这样才可以监控到是否已加群！','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'群名','type'=>'string'),
			array('name'=>'owner','title'=>'归属成员','type'=>'string'),
			array('name'=>'num','title'=>'群人数','type'=>'string'),
			array('name'=>'status','title'=>'群主状态','type'=>'radio','options'=>'qunstatus'),
			array('name'=>'isdel','title'=>'群状态','type'=>'radio','options'=>'isdel'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'url','title'=>'加群链接','type'=>'copy'),
			array('name'=>'welcomeurl','title'=>'群欢迎语链接','type'=>'copy'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_scrm/member.qunlist/updata/','title'=>'同步客户群'),
		),'action'=>array(
			array('type'=>'href','op'=>'setewm','title'=>'设置群二维码/加入群H5链接'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'群名称','type'=>'string','width'=>'15'),
			array('type'=>'choose','width'=>15,'title'=>'部门成员','name'=>'workerid','action'=>'jy_scrm/worker.get_worker/'),
		),	
	);
	$return['isdel']=array(
		'0'=>'正常',
		'1'=>'已解散',
	);
	$return['qunstatus']=$this->qunstatus;
	$return['workers']=$this->getall('worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setewm'){
	$return=array();
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'ewm'=>$post['ewm'],
			'ewmurl'=>$post['ewmurl'],
		);
		pdo_update('jy_scrm_qun',$data,array('id'=>$id));
		exi('设置成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_qun',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择群！','error');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
				array('type'=>'image','title'=>'群加入二维码','name'=>'ewm','help'=>'移步企业微信--客户联系--加客户--加入群聊--新建二维码--下载二维码'),
				array('type'=>'string','title'=>'群加入二维码链接地址','name'=>'ewmurl','help'=>'把上面的群加入二维码，通过https://jiema.wwei.cn/在线解码，把解码到的链接，填入此处！这样方便点击链接实现加入企业微信群！'),					
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_qun',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_scrm_qun',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='updata'){
	$workers=pdo_getall('jy_scrm_worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','userid'));
	if(count($workers)<=0){
		exi('请先同步通讯录！','error');
	}
	//$this->model('model')->check_apilog('get_qunlist',true);
	$this->model('member')->get_qunlist('','','',true);
	$this->model('model')->add_apilog('get_qunlist');
	exi('同步群成功！');
}